﻿using System;
using System.IO;
using System.Data;
using System.Collections.Generic;
using System.Collections;
using DevExpress.Web.ASPxCallback;

using YhmOA.Model;
using YhmOA.Common;
using YhmOA.BLL;
using YhmOA.Common.Extensions;

namespace YhmOA.Integrated.Car
{
    public partial class TravelRecordForm : FormPage<Integrated_Car_TravelRecord>
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //是否为查看
                bool isReadOnly = Mode == FormMode.ReadOnly;
                //绑定控件的数据源
                if (!isReadOnly)
                {
                    //绑定车牌号码
                    List<string> plateNumList = Integrated_Car_CarBLL.GetPlateNumList();
                    this.txt_PlateNum.Items.AddRange(plateNumList);
                    //绑定司机姓名
                    List<string> driverNameList = Integrated_Car_DriverBLL.GetNameList();
                    this.txt_UserName.Items.AddRange(driverNameList);
                    this.txt_Driver.Items.AddRange(driverNameList);
                    //绑定去向路线
                    this.txt_Direction.DataSource = Integrated_Car_DicBLL.GetDirectionList();
                    this.txt_Direction.DataBind();
                }
                this.btnCancel.Visible = !isReadOnly;
                this.btnSave.Visible = !isReadOnly;
                //初始化数据
                base.LoadData(isReadOnly);

                //修改或添加，对内容页做出处理
                if (!isReadOnly)
                {
                    HidField["oldPlateNum"] = this.txt_PlateNum.Text;
                    
                    if (Mode == FormMode.Modify)
                    {
                        HidField["SN"] = Local.SN;
                        HidField["LastReturnMileage"] = Integrated_Car_TravelRecordBLL.GetPreInfoByPlateNum(this.txt_PlateNum.Text, this._ID).ReturnMileage;
                        //只有最新一条、并且该条记录非补充冲销，可以修改里程和车牌号码
                        if (this._ID != Integrated_Car_TravelRecordBLL.GetLastInfoByPlateNum(this.txt_PlateNum.Text).ID || Local.Flag != 0)
                        {
                            this.txt_PlateNum.ReadOnly = true;
                            this.txt_SendMileage.ReadOnly = true;
                            this.txt_ReturnMileage.ReadOnly = true;
                            this.txt_CurrentMileage.ReadOnly = true;
                        }
                        if (Local.Flag != 0)
                        {
                            this.btnSave.Enabled = false;
                            this.btnCancel.Enabled = false;
                        }
                    }
                } 
            }
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <returns>保存是否成功</returns>
        private bool save(bool isBJ, bool isCX, decimal mileage)
        {
            bool isSucceed = false;
            Local = CreateInstance();
            
            switch (Mode)
            {
                case FormMode.ReadOnly:
                    break;
                case FormMode.Add:
                    Local.InsertDate = DateTime.Now;
                    Local.SN = Local.UserName.ToShortPY() + Local.InsertDate.ToString("yyyyMMddHHmmss") + Local.Cause.Substring(0, 2).ToShortPY();
                    isSucceed = Integrated_Car_TravelRecordBLL.Save(isBJ, isCX, mileage, Local);
                    break;
                case FormMode.Modify:
                    Local.UpdateDate = DateTime.Now;
                    Local.SN = HidField["SN"].ToString();
                    isSucceed = Integrated_Car_TravelRecordBLL.Update(isBJ, isCX, mileage, Local);
                    break;
                default:
                    break;
            }

            return isSucceed;
        }

        protected void cbSave_Callback(object source, CallbackEventArgs e)
        {
            ASPxCallback callbackControl = source as ASPxCallback;
            bool isSucceed = false;
            isSucceed = save(bool.Parse(e.Parameter.Split('|')[0]), bool.Parse(e.Parameter.Split('|')[1]), decimal.Parse(e.Parameter.Split('|')[2]));
            callbackControl.JSProperties["cpIsSucceed"] = isSucceed;
        }

        /// <summary>
        /// 车牌号码改变后的回调，取得该车牌号码的最后一次行车记录的里程或者上次行车记录的里程
        /// 如果页面状态为FormMode.Add，那么取得该车牌号码的最后一次行车记录
        /// 如果页面状态为FormMode.Modify，此时分两种情况
        /// 1.该车牌号码和原始行车记录的车牌号码一致，则取得该车牌号码的上一次行车记录
        /// 2.该车牌号码和原始行车记录的车牌号码不一致，则取得该车牌号码的最后一次行车记录
        /// </summary>
        /// <param name="source">source</param>
        /// <param name="e">e</param>
        protected void cbPlateNum_Callback(object source, CallbackEventArgs e)
        {
            ASPxCallback callbackControl = source as ASPxCallback;
            decimal mileageStr = 0;
            if (Mode == FormMode.Add)
                mileageStr = Integrated_Car_TravelRecordBLL.GetLastInfoByPlateNum(e.Parameter).ReturnMileage;
            else if (Mode == FormMode.Modify)
            {
                
                if (HidField["oldPlateNum"].ToString() == this.txt_PlateNum.Text)
                    mileageStr = Integrated_Car_TravelRecordBLL.GetPreInfoByPlateNum(e.Parameter,this._ID).ReturnMileage;
                else
                    mileageStr = Integrated_Car_TravelRecordBLL.GetLastInfoByPlateNum(e.Parameter).ReturnMileage; ;
            }
            callbackControl.JSProperties["cpLastReturnMileage"] = mileageStr; 
        }
    }
}